package com.cap.dreamcircle.View;

import android.app.Dialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.IUserModel;
import com.cap.dreamcircle.Model.Impl.UserModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.DisplayUtil;
import com.cap.dreamcircle.Utils.RxCountDown;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.Utils.TransformUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.Subscription;
import rx.functions.Action0;

/**
 * Created by liucxu on 2018/3/16.
 */

public class SettingBindPhoneNumActivity extends BaseActivity {

    @BindView(R.id.imv_back)
    ImageView imv_back;
    @BindView(R.id.edt_phone_num)
    EditText edt_phone_num;
    @BindView(R.id.edt_validate_code)
    EditText edt_validate_code;
    @BindView(R.id.tv_request_bind_code)
    TextView tv_request_bind_code;
    @BindView(R.id.tv_bind_phone_num)
    TextView tv_bind_phone_num;

    private TextView tv_cancel;
    private TextView tv_bind;

    private IUserModel userModel;
    private Subscription subscription;
    private int phoneState;

    private View dialogView;
    private Dialog notice_bind_dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting_bind_phone_num;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        userModel = new UserModelImpl();
        dialogView=View.inflate(SettingBindPhoneNumActivity.this, R.layout.dialog_bind_notify, null);
        initDialoigViews();
        initBindDialog();
    }

    @Override
    public void setupData() {

    }

    private void initDialoigViews() {
        tv_cancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
        tv_bind = (TextView) dialogView.findViewById(R.id.tv_bind);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notice_bind_dialog.dismiss();
            }
        });
        tv_bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BindPhoneWithCode();
            }
        });
    }

    private void initBindDialog() {
        notice_bind_dialog = new Dialog(this, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = notice_bind_dialog.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(SettingBindPhoneNumActivity.this, 40);
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        notice_bind_dialog.setCancelable(false);
        notice_bind_dialog.setContentView(dialogView);
    }

    private void getVerificationCode() {
        userModel.BindVerificationCode(edt_phone_num.getText().toString()).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
                tv_request_bind_code.setEnabled(false);
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                hideProgress();
                tv_request_bind_code.setEnabled(false);
                subscription = RxCountDown.countdown(60).subscribe(new Observer<Integer>() {
                    @Override
                    public void onCompleted() {
                        tv_request_bind_code.setEnabled(true);
                        tv_request_bind_code.setText("发送");
                    }

                    @Override
                    public void onError(Throwable e) {
                        tv_request_bind_code.setEnabled(true);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        tv_request_bind_code.setEnabled(false);
                        tv_request_bind_code.setText(integer + "S");
                    }
                });


            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                if (response.getCode() == 0 || response.getCode() == 1) {
                    phoneState = response.getCode();
                    subscription = RxCountDown.countdown(60).subscribe(new Observer<Integer>() {
                        @Override
                        public void onCompleted() {
                            tv_request_bind_code.setEnabled(true);
                            tv_request_bind_code.setText("发送");
                        }

                        @Override
                        public void onError(Throwable e) {
                            tv_request_bind_code.setEnabled(true);
                        }

                        @Override
                        public void onNext(Integer integer) {
                            tv_request_bind_code.setEnabled(false);
                            tv_request_bind_code.setText(integer + "S");
                        }
                    });
                } else {
                    ToastUtils.showShortToast(SettingBindPhoneNumActivity.this, response.getDesc());
                    tv_request_bind_code.setEnabled(true);
                }
            }
        });
    }

    private void BindPhoneWithCode() {
        userModel.BindPhoneWithCode(edt_phone_num.getText().toString(), edt_validate_code.getText().toString()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                ToastUtils.showShortToast(SettingBindPhoneNumActivity.this, "绑定手机号码成功");
                notice_bind_dialog.dismiss();
                hideProgress();
                App.GetInstance().finishActivity(SettingBindPhoneNumActivity.this);
                App.GetInstance().finishClassActivity(ChangePhoneNumActivity.class);
            }

            @Override
            public void onFailure(BaseBean response) {
                ToastUtils.showShortToast(SettingBindPhoneNumActivity.this, response.getDesc());
                notice_bind_dialog.dismiss();
                hideProgress();
//                App.GetInstance().finishActivity(SettingBindPhoneNumActivity.this);
//                App.GetInstance().finishClassActivity(ChangePhoneNumActivity.class);
            }
        });
    }

    @OnClick({R.id.tv_request_bind_code, R.id.tv_bind_phone_num, R.id.imv_back})
    void doClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_request_bind_code:
                if (TextUtils.isEmpty(edt_phone_num.getText().toString())) {
                    ToastUtils.showShortToast(SettingBindPhoneNumActivity.this, "请输入手机号码");
                } else {
                    getVerificationCode();
                }
                break;
            case R.id.tv_bind_phone_num:
                if (TextUtils.isEmpty(edt_phone_num.getText().toString())) {
                    ToastUtils.showShortToast(SettingBindPhoneNumActivity.this, "请输入手机号码");
                } else {
                    if (TextUtils.isEmpty(edt_validate_code.getText().toString())) {
                        ToastUtils.showShortToast(SettingBindPhoneNumActivity.this, "请输入验证码");
                    } else {
                        if (phoneState == 0) {
                            BindPhoneWithCode();
                        } else if (phoneState == 1) {
                            notice_bind_dialog.show();
                        }
                    }
                }
                break;
            case R.id.imv_back:
                App.GetInstance().finishActivity(SettingBindPhoneNumActivity.this);
                break;
        }
    }

    @Override
    protected void onStop() {
        if (subscription != null) {
            subscription.unsubscribe();
        }
        super.onStop();
    }
}
